package com.linys.scala.qf.day02

/**
  * Created by admin on 2019/10/16.
  */
object WordCount {

  def main(args: Array[String]): Unit = {

    val lines = List("hello java hello  c++ python hello scala scala",
      "linys hello python c++ hello scala scala",
      "hello java hello scala",
      "hello  python hello c++ c++")

    println("初始化：" + lines)
    // 切分压平
    val words = lines.flatMap(_.split(" "))
    println("切分压平：" + words)
    // 过滤
    val wordFilter = words.filter(!_.isEmpty)
    println("过滤：" + wordFilter)
    // 把每个单词再生产一个一个的map
    val tuples = wordFilter.map((_, 1))
    println("产生map：" + tuples)
    // 分组：以key单词进行分组
    val groups = tuples.groupBy(_._1)
    println("单词分组：" + groups)
    // 统计组内个数
    val wordCount = groups.mapValues(_.size)
    println("统计组内个数：" + wordCount)
    // 排序
    val wordCountSorted = wordCount.toList.sortBy(_._2).reverse
    println("排序：" + wordCountSorted)


    println("再次计算最终结果：" + lines.flatMap(_.split(" ")).filter(!_.isEmpty).map((_, 1)).groupBy(_._1).mapValues(_.size).toList.sortBy(_._2).reverse)

  }

}
